using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CarRentalSystem.Dtos;
using CarRentalSystem.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AutoMapper;

namespace CarRentalSystem.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class CarController : ControllerBase
    {
        private readonly CarContext _context;
        private readonly IMapper _mapper;

        public CarController(CarContext context, IMapper mapper)
        {
            this._context = context;
            _mapper = mapper;
        }

        [HttpGet]
        public IActionResult Cars()
        //显示数据
        {
            var res = _context.Cars.Include(c => c.UserModel).Where(c => !c.IsDeleted).Select(c => new
            {
                CarId = c.CarId,
                CarPhoto = c.CarPhoto,
                CarType = c.CarType,
                CarStaus = c.CarStaus
            }).ToList();

            if (res != null)
            {
                return Ok(res);
            };
            return NotFound();
        }
        //搜索
        [HttpGet("search")]
        public IActionResult SearchCars(int carId = 0, string carType = null, bool? carStatus = null)
        {
            var query = _context.Cars.Include(c => c.UserModel).Where(c => !c.IsDeleted).AsQueryable();

            if (carId != 0)
            {
                query = query.Where(c => c.CarId == carId);
            }
            else
            {
                return BadRequest("Please provide a valid carId.");
            }

            if (!string.IsNullOrEmpty(carType))
            {
                query = query.Where(c => c.CarType == carType);
            }

            if (carStatus.HasValue)
            {
                query = query.Where(c => c.CarStaus.Equals(carStatus.Value));
            }

            var res = query.Select(c => new
            {
                CarId = c.CarId,
                CarPhoto = c.CarPhoto,
                CarType = c.CarType,
                CarStaus = c.CarStaus
            }).ToList();

            if (res.Count > 0)
            {
                return Ok(res);
            }

            return Ok();
        }

        //增
        [HttpPost]
        public IActionResult AddCar([FromBody] CarForCreateDto carForCreateDto)
        {
            var car = _mapper.Map<CarsModel>(carForCreateDto);

            _context.Cars.Add(car);
            _context.SaveChanges();
            return Ok(car);
        }

        //改
        [HttpPut("{id}")]
        public IActionResult UpdateCar(int id, [FromBody] CarForCreateDto updatedCar)
        {
            var car = _context.Cars.Find(id);
            if (car != null && !car.IsDeleted)
            {
                car.CarPhoto = updatedCar.CarPhoto;
                car.CarType = updatedCar.CarType;
                car.CarStaus = updatedCar.CarStaus;
                _context.Update(car);
                _context.SaveChanges();
                return Ok(car);
            }
            return NotFound();
        }

        //软删
        [HttpDelete("{id}")]
        public IActionResult DeleteCar(int id)
        {
            var car = _context.Cars.Find(id);
            if (car != null)
            {
                var res = _context.Cars.Find(id);
                // _context.Cars.Remove(car);
                res.IsDeleted = true;
                _context.Update(res);
                _context.SaveChanges();
                return Ok();
            }
            return NotFound();
        }
    }
}